(function ($) {
    $.fn.stepByStepKyers = function (options) {
        var stepLi = $(this);
        var defaults = { "paymentitem": ".payment-item", "delay": "500" };
        var settings = $.extend(defaults, options || {});
        //Thay đổi chiều dài các tab item
        $(window).on("load resize", function () {
            var widthItem = parseFloat($(".list-step-bullet").width()) / parseFloat($(".list-step-bullet").find("li").length) - 1;
            $(".list-step-bullet").find("li").css({ "width": widthItem });
        });
        //Thêm class color-inside
        var colorSlide = $("<div/>", {
            "class": "color-slide"
        });
        var colorInside = $("<div/>", {
            "class": "color-inside"
        });
        var stepBullet = $("<div/>", {
            "class": "step-bullet"
        });

        $(stepLi).append(colorSlide);
        $(".color-slide").append(colorInside).append(stepBullet);
        //>> Add attribute Index for Tab Item
        var i = 1;
        $.each($(stepLi), function () {
            $(this).attr("index", i++);
        });
        //>> Add Attribute Index for Tab Content
        i = 1;
        $.each($(settings.paymentitem), function () {
            $(this).attr("index", i++);
        });

        var itemPayment = $(this);
        //>> Add Class active cho Tab Item First vaf Tab Content First
        $(stepLi).first().addClass("active");
        $(settings.paymentitem).first().addClass("active");
        //Sử lý sự kiện click vào các tab item
        $(this).on("click", function () {
            var paymentWidth = parseFloat($(settings.paymentitem).width());
            var currIndex = $(stepLi).parent().find(".active").last().attr("index");
            var index = $(this).attr("index");
            if ($(this).attr("index") > currIndex) {
                //>>> Go to Next Tab Item
                $(this).addClass("active");
                $(this).prevAll().addClass("active");
                $(this).prevAll().find(".color-inside").css({ "width": "100%" });
                $(this).find(".color-inside").css({ "width": "0px" });
                $(this).find(".color-inside").animate({ "width": "100%" }, settings.delay);
                //>>> Go to Next Tab Content
                $(settings.paymentitem).removeClass("active");
                $(settings.paymentitem).css({ "left": "auto", "right": paymentWidth * -1 });

                $(settings.paymentitem).children().parent("li[index=" + index + "]").animate({ "right": "0px" }).addClass("active");
                //>>Show and Hide Buttons Control
                if (index > 1) {
                    $(".btn-preview").removeClass("hidden");
                }
                if ($(settings.paymentitem).children().parent("li.active").next().length <= 0) {
                    $(".btn-next").addClass("hidden");
                    $(".btn-finish").removeClass("hidden");
                }
            }
            else {
                //Back to preview Tab Item
                $(this).next().nextAll().removeClass("active");
                $(this).next().find(".color-inside").animate({ "width": "0" }, settings.delay, function () {
                    $(this).parent().parent("li").removeClass("active");
                });
                //>>> Back to Preview Tab Content
                if ($(this).attr("index") < currIndex) {
                    $(settings.paymentitem).removeClass("active");
                    $(settings.paymentitem).css({ "left": paymentWidth * -1, "right": "auto" });
                    $(settings.paymentitem).children().parent("li[index=" + index + "]").animate({ "left": "0px" }).addClass("active");
                }
                //>> Show and Hide Button Controls
                $(".btn-next").removeClass("hidden");
                $(".btn-finish").addClass("hidden");
                if ($(settings.paymentitem).children().parent("li.active").prev().length <= 0) {
                    $(".btn-preview").addClass("hidden");
                }
            }
        });

        $(".btn-next").on("click", function () {
            $(".btn-preview").removeClass("hidden");
            var paymentWidth = parseFloat($(settings.paymentitem).width());
            //>>>Go to Next Tab Content
            $(settings.paymentitem).children().parent("li").css({ "left": "auto", "right": paymentWidth * -1 });
            $(settings.paymentitem).children().parent("li.active").next().animate({ "right": "0" }).addClass("active");
            $(settings.paymentitem).children().parent("li.active").prev().removeClass("active");
            //>>Go to Next Tab Item
            stepLi.children().parent("li.active").next().addClass("active");
            stepLi.children().parent("li.active:last").find(".color-inside").css("width", "0");
            stepLi.children().parent("li.active:last").find(".color-inside").animate({ "width": "100%" }, settings.delay);
            //>> Show and Hide Button Controls
            if ($(settings.paymentitem).children().parent("li.active").next().length <= 0) {
                $(this).addClass("hidden");
                $(".btn-finish").removeClass("hidden");
            }
        });
        $(".btn-preview").on("click", function () {
            //>> Show and Hide Button Controls
            $(".btn-next").removeClass("hidden");
            $(".btn-finish").addClass("hidden");
            //>> Go to Preview Tab Content
            var paymentWidth = parseFloat($(settings.paymentitem).width());
            $(settings.paymentitem).children().parent("li").css({ "right": "auto", "left": paymentWidth * -1 });
            $(settings.paymentitem).children().parent("li.active").prev().animate({ "left": "0px" }).addClass("active");
            $(settings.paymentitem).children().parent("li.active").next().removeClass("active");
            //>> Go to Preivew Tab Item
            if (stepLi.children().parent("li:not(.active)").length <= 0) {
                stepLi.children().parent("li:last").find(".color-inside").animate({ "width": "0" }, settings.delay, function () {
                    $(this).parent().parent("li").removeClass("active");
                });
            }
            else if (stepLi.children().parent("li.active").length > 1) {
                stepLi.children().parent("li:not(.active)").prev().find(".color-inside").animate({ "width": "0" }, settings.delay, function () {
                    $(this).parent().parent("li").removeClass("active");
                });
            }
            //>>--------------------
            stepLi.children().parent("li.active").prev().addClass("active");
            if ($(settings.paymentitem).children().parent("li.active").prev().length <= 0) {
                $(this).addClass("hidden");
            }
        });
    }
    ///Xử lý show Login khi nhấn Login ở Payment
    $("#second-btn-login").on("click", function () {
        $("html, body").animate({ scrollTop: 0 }, '0');
        $(".login-top.top-show-dropdown").find(".top-dropdown").slideDown(600, function () {
            $(".login-top.top-show-dropdown").addClass("active");
        });
    });
    $(document).on("click", function (e) {
        if (!$(e.target).closest($(".login-top.top-show-dropdown")).length && !$(e.target).closest($("#second-btn-login")).length) {
            $(".login-top.top-show-dropdown").find(".top-dropdown").slideUp(100, function () {
                $(this).css({"display": ""});
                $(".login-top.top-show-dropdown").removeClass("active");
            });
        }
    }); $.fn.UsingInfo = function () {
        $(this).change(function () {
            if ($(this).is(":checked")) {
                $.ajax({
                    type: "POST",
                    url: '/Payment/GetInfo/',
                    traditional: true,
                    dataType: 'json',
                    complete: function (edata) {
                        var obj = $.parseJSON(edata.responseText);
                        $(".txtname").val(obj.Name);
                        $(".txtaddress").val(obj.Address);
                        $(".txtphone").val(obj.Phone);
                        $(".txtemail").val(obj.Email);
                        $('.txtcustomer').text(obj.Name);
                        $('.txtaddresscustomer').text(obj.Address);
                        $('.txtemailcustomer').text(obj.Email);
                        $('.txtphonenumcustomer').text(obj.Phone);
                    }
                });
            }
            else {
                $(".txtname").val("");
                $(".txtaddress").val("");
                $(".txtphone").val("");
                $(".txtemail").val("");
                jQuery('.txtcustomer').text("");
                jQuery('.txtaddresscustomer').text("");
                jQuery('.txtemailcustomer').text("");
                jQuery('.txtphonenumcustomer').text("");
            }
            //'unchecked' event code
        });
    };
} (jQuery));
(function($){
	$(document).ready(function(e) {
	   	$(".list-step-bullet li").stepByStepKyers({ "paymentitem": ".payment-item" });
        $("#chk-use-info").UsingInfo();
	});
}(jQuery));